package com.controller.admin;

import com.entity.BlogType;
import com.entity.Comment;
import com.service.BlogService;
import com.service.BlogTypeService;
import com.service.CommentService;
import com.util.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  管理员博客类别Controller层
 *  @author Administrator
 */
@Controller
@RequestMapping("/admin/comment")
public class CommentAdminController {
    @Resource
    private CommentService commentService;

    @Resource
    private BlogService blogService;

    /**
     * 分页查询评论信息
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value="offset",required=false)int offset,@RequestParam(value="limit",required=false)Integer limit,HttpServletResponse response)throws Exception{
        Map<String,Object> map = new HashMap();
        limit=limit+offset;
        map.put("size", limit);
        map.put("start", offset);
        List<Comment> CommentList=commentService.list(map);
        System.out.println("获取到的："+CommentList);
        Long total=commentService.getTotal(map);
        JSONObject result=new JSONObject();
        JSONArray jsonArray= JSONArray.fromObject(CommentList);
        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(response, result);
        return null;
    }


    /**
     * 博客类别信息删除
     * @param ids
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete")
    public String delete(@RequestParam(value="ids",required=false)String ids, HttpServletResponse response)throws Exception{
        String []idsStr=ids.split(",");
        JSONObject result=new JSONObject();
        for(int i=0;i<idsStr.length;i++){
                commentService.delete(Integer.parseInt(idsStr[i]));
            }
        result.put("success", true);
        ResponseUtil.write(response, result);
        return null;
    }
}
